//
//  NSDate+format.m
//  
//
//  Created by JIRENTIANXIANG on 6/12/15.
//
//

#import "NSDate+format.h"
#import "NSDate+range.h"

@implementation NSDate (format)

-(NSString*)year
{
    NSDateComponents* components=[self dateComponents];
    NSInteger year=components.year;
    return [NSString stringWithFormat:@"%d",(int)year];
}

-(NSString*)month
{
    NSDateComponents* components=[self dateComponents];
    NSInteger month=components.month;
    return [NSString stringWithFormat:month<10?@"0%d":@"%d",(int)month];
}

-(NSString*)day
{
    NSDateComponents* components=[self dateComponents];
    NSInteger day=components.day;
    return [NSString stringWithFormat:day<10?@"0%ld":@"%ld",(long)day];
}

-(NSString*)yearMonthDay
{
    return [NSString stringWithFormat:@"%@%@%@",self.year,self.month,self.day];
}

-(NSDateComponents*)dateComponents
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitWeekday;
    NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:self];
    return dateComponents;
}

-(NSString*)year_month_day
{
    NSDateComponents* components=[self dateComponents];
    NSInteger year=components.year;
    return [NSString stringWithFormat:@"%d-%@",(int)year,self.month_day];
}

-(NSString*)month_day
{
    NSDateComponents* components=[self dateComponents];
    NSInteger month=components.month;
    NSInteger day=components.day;
    NSString* monthDisplay=[NSString stringWithFormat:month<10?@"0%d":@"%d",(int)month];
    NSString* dayDisplay=[NSString stringWithFormat:day<10?@"0%ld":@"%ld",(long)day];
    return [NSString stringWithFormat:@"%@-%@",monthDisplay,dayDisplay];
}

-(NSString*)hour_minute_second
{
    NSDateComponents* components=[self dateComponents];
    NSInteger hour=components.hour;
    NSInteger minte=components.minute;
    NSInteger seconod=components.second;
    NSString* hourDisplay=[NSString stringWithFormat:hour<10?@"0%d":@"%d",(int)hour];
    NSString* minuteDisplay=[NSString stringWithFormat:minte<10?@"0%d":@"%d",(int)minte];
    NSString* secondDisplay=[NSString stringWithFormat:seconod<10?@"0%d":@"%d",(int)seconod];
    return [NSString stringWithFormat:@"%@:%@:%@",hourDisplay,minuteDisplay,secondDisplay];
}

-(NSString*)hour_minute
{
    NSDateComponents* components=[self dateComponents];
    NSInteger hour=components.hour;
    NSInteger minte=components.minute;
    NSString* hourDisplay=[NSString stringWithFormat:hour<10?@"0%ld":@"%ld",(long)hour];
    NSString* minuteDisplay=[NSString stringWithFormat:minte<10?@"0%ld":@"%ld",(long)minte];
    return [NSString stringWithFormat:@"%@:%@",hourDisplay,minuteDisplay];
}

-(NSString*)minute_second
{
    NSDateComponents* components=[self dateComponents];
    NSInteger minte=components.minute;
    NSInteger seconod=components.second;
    NSString* minuteDisplay=[NSString stringWithFormat:minte<10?@"0%ld":@"%ld",(long)minte];
    NSString* secondeDisplay=[NSString stringWithFormat:seconod<10?@"0%ld":@"%ld",(long)seconod];
    return [NSString stringWithFormat:@"%@:%@",minuteDisplay,secondeDisplay];
}

-(NSString*)postFormat
{
    if (![self isPast]) {
        return @"";
    }
    if ([self isPastWithinOneMinute]) {
        return @"刚刚";
    }
    if ([self isPastWithinOneHour]) {
        return [NSString stringWithFormat:@"%d分钟前",(-(int)[self timeIntervalSinceNow])/60];
    }
    if ([self isPastWithinToday]) {
        return [NSString stringWithFormat:@"%d小时前",(-(int)[self timeIntervalSinceNow])/3600];
    }
    if ([self isWithinYesterday]) {
        NSString* h_m_s=self.hour_minute_second;
        return [NSString stringWithFormat:@"昨天%@",h_m_s];
    }
    if ([self isWithinTheDayBeforeYesterday]) {
        NSString* h_m_s=self.hour_minute_second;
        return [NSString stringWithFormat:@"前天%@",h_m_s];
    }
    return self.month_day_chinese;
}

-(NSString*)weekday
{
    NSDateComponents* components=[self dateComponents];
    NSInteger weekday=components.weekday;
    switch (weekday) {
        case 1:
            return @"周日";
        case 2:
            return @"周一";
        case 3:
            return @"周二";
        case 4:
            return @"周三";
        case 5:
            return @"周四";
        case 6:
            return @"周五";
        case 7:
            return @"周六";
        default:
            return @"";
    }
}

-(NSString*)month_day_chinese
{
    NSDateComponents* components=[self dateComponents];
    NSInteger month=components.month;
    NSInteger day=components.day;
    NSString* monthDisplay=[NSString stringWithFormat:month<10?@"0%ld":@"%ld",(long)month];
    NSString* dayDisplay=[NSString stringWithFormat:day<10?@"0%ld":@"%ld",(long)day];
    return [NSString stringWithFormat:@"%@月%@日",monthDisplay,dayDisplay];
}

@end
